package cn.kgc.watch.service.impl;

import cn.kgc.watch.entity.CommoditySku;
import cn.kgc.watch.entity.CommoditySpu;
import cn.kgc.watch.entity.Specification;
import cn.kgc.watch.entity.vo.PageInfoVo;
import cn.kgc.watch.mapper.CommoditySkuMapper;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import cn.kgc.watch.mapper.CommoditySpuMapper;
import cn.kgc.watch.service.CommoditySpuService;

import java.util.List;
import java.util.Map;

@Service
public class CommoditySpuServiceImpl implements CommoditySpuService{

    @Resource
    private CommoditySpuMapper commoditySpuMapper;
    @Resource
    private CommoditySkuMapper commoditySkuMapper;

    @Override
    public void add(CommoditySpu commoditySpu) {
        this.commoditySpuMapper.insert(commoditySpu);
        CommoditySku commoditySku = commoditySpu.getCommoditySku();
        commoditySku.setCommoditySpu(commoditySpu);
        this.commoditySkuMapper.insert(commoditySku);
    }

    @Override
    public PageInfo<CommoditySpu> findAll(Specification specification) {
        PageHelper.startPage(specification.getPageNum(),specification.getPageSize());
        List<CommoditySpu> commoditySpuList=this.commoditySpuMapper.selectAll(specification);
        for (CommoditySpu c:commoditySpuList){
            System.out.println("c = " + c);
        }
        //将Specifiation 转换成jsOn
        for(CommoditySpu commoditySpu:commoditySpuList){
            for (CommoditySku commoditySku:commoditySpu.getCommoditySkuList()){
                Specification specification01 =JSONObject.parseObject(commoditySku.getSpecification(),Specification.class);
                commoditySku.setSpecificationObject(specification01);
            }
        }
        PageInfo<CommoditySpu> pageInfo = new PageInfo<>(commoditySpuList);
        return pageInfo;
    }

    @Override
    public PageInfo<CommoditySpu> findPage(PageInfoVo pageInfoVo) {
        PageHelper.startPage(pageInfoVo.getPageNum(),pageInfoVo.getPageSize());
        List<CommoditySpu> commoditySpuList = this.commoditySpuMapper.selectPage(pageInfoVo);
        for(CommoditySpu commoditySpu:commoditySpuList){
            for(CommoditySku commoditySku:commoditySpu.getCommoditySkuList()){
                Specification specification = JSONObject.parseObject(commoditySku.getSpecification(),Specification.class);
                commoditySku.setSpecificationObject(specification);
            }
        }
        PageInfo<CommoditySpu> pageInfo = new PageInfo<>(commoditySpuList);
        return pageInfo;
    }

    @Override
    public CommoditySpu findById(Integer id) {
          CommoditySpu commoditySpu = this.commoditySpuMapper.selectById(id);
          for(CommoditySku commoditySku:commoditySpu.getCommoditySkuList()){
              Specification specification = JSONObject.parseObject(commoditySku.getSpecification(),Specification.class);
              commoditySku.setSpecificationObject(specification);
          }
        return commoditySpu ;
    }


}
